package cn.swust.designpatternlearningplatform.controller;

import cn.swust.designpatternlearningplatform.common.AnnotationUtils.Authentication;
import cn.swust.designpatternlearningplatform.common.Result.PageInfo;
import cn.swust.designpatternlearningplatform.common.Result.R;
import cn.swust.designpatternlearningplatform.common.utils.AuthUserContext;
import cn.swust.designpatternlearningplatform.common.utils.JwtUtils;
import cn.swust.designpatternlearningplatform.domain.bo.StudentTopicTypeBo;
import cn.swust.designpatternlearningplatform.domain.dto.TopicCollectDto;
import cn.swust.designpatternlearningplatform.domain.enums.TopicTypeEnum;
import cn.swust.designpatternlearningplatform.domain.enums.TopicUpdateTypeEnum;
import cn.swust.designpatternlearningplatform.domain.vo.TopicCollectVo;
import cn.swust.designpatternlearningplatform.service.TopicCollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/topic")
public class TopicCollectController {
    /**
     * 题目收藏
     *
     * @author Tang
     * @date 2023/5/2 23:04
     **/
    @Resource
    private TopicCollectService topicCollectService;

    /**
     * 取消收藏
     *
     * @param token
     * @param topicCollectVo
     * @return cn.swust.designpatternlearningplatform.common.Result.R<java.lang.Object>
     * @author Tang
     * @date 2023/7/8 13:20
     **/
    @Authentication
    @DeleteMapping("/collect")
    public R<Object> cancelCollect(@RequestHeader("token") String token, @RequestBody TopicCollectVo topicCollectVo) {
        //学生的学号
        String studentNumber = JwtUtils.getClaims(token).get(JwtUtils.NUMBER, String.class);
        topicCollectService.deleteCollectTopic(studentNumber, topicCollectVo.getTopicIds());
        return R.success("取消收藏");
    }

    /**
     * 获取收藏列表
     *
     * @return cn.swust.designpatternlearningplatform.common.Result.R<cn.swust.designpatternlearningplatform.domain.dto.PaperDto>
     * @author Tang
     * @date 2023/7/8 13:20
     **/
    @Authentication
    @GetMapping("/collect/list")
    public R<PageInfo<TopicCollectDto>> collectList(@RequestParam("currentPage") Integer currentPage,
                                                    @RequestParam("pageSize") Integer pageSize,
                                                    @RequestParam(value = "topicType", required = false) Integer topicType) {
        if (currentPage <= 0) {
            return R.success("页码错误");
        }
        String number = AuthUserContext.get().getNumber();
        StudentTopicTypeBo studentTopicTypeBo = new StudentTopicTypeBo();
        studentTopicTypeBo.setCurrent(currentPage);
        studentTopicTypeBo.setSize(pageSize);
        studentTopicTypeBo.setTopicType(TopicTypeEnum.topicType(topicType));
        studentTopicTypeBo.setStudentNumber(number);
        studentTopicTypeBo.setUpdateType(TopicUpdateTypeEnum.COLLECT.getTypeName());
        PageInfo<TopicCollectDto> collectQuestionByStudentNumber = topicCollectService.
                getCollectQuestionByStudentNumber(studentTopicTypeBo);
        return R.success(collectQuestionByStudentNumber);
    }

    /**
     * 添加收藏
     *
     * @param topicCollectVo
     * @return cn.swust.designpatternlearningplatform.common.Result.R<java.lang.String>
     * @author Tang
     * @date 2023/7/10 13:52
     **/
    @Authentication
    @PostMapping("/collect")
    public R<String> collectTopic(@RequestBody TopicCollectVo topicCollectVo) {
        String studentNumber = AuthUserContext.get().getNumber();
        topicCollectService.addCollectTopic(studentNumber, topicCollectVo.getTopicIds());
        return R.success("收藏成功");
    }
}
